//
//  ProductNew.m
//  Trading365
//
//  Created by Jason on 12-3-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ChanceAdd.h"
#import "QuotEditCell.h"
#import "QuotSelectList.h"

@implementation ChanceAdd
@synthesize table;

static NSMutableDictionary *InfoNameDict;
static NSMutableDictionary *InfoCodeDict;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title=INFO_STRING_CHANCE_ADD;
    [self.table setBackgroundColor:BACKGROUND_COLOR];

    self.navigationItem.rightBarButtonItem.enabled=YES;

    NSArray *title_array = [[NSArray alloc] initWithObjects:@"商机编号",@"商机名称",@"客户",@"业务员",nil];
    self.InfoListTitle = title_array; 
    [title_array release];

    NSArray *name_title = [[NSArray alloc] initWithObjects:@"chance_no",@"chance_name",@"customer_id",@"employee_id",nil];
    self.InfoListName = name_title; 
    [name_title release];
    
    InfoNameDict = [[NSMutableDictionary alloc] initWithCapacity:0];
    InfoCodeDict = [[NSMutableDictionary alloc] initWithCapacity:0];
    
    [InfoCodeDict setValue:@"" forKey:@"0,0"];//@"chance_no",
    [InfoCodeDict setValue:@"" forKey:@"0,1"];//@"chance_name",
    [InfoCodeDict setValue:@"" forKey:@"0,2"];//@"customer_id"
    [InfoCodeDict setValue:[[Session share] getMemberID] forKey:@"0,3"];//@"employee_id",
    [InfoNameDict setValuesForKeysWithDictionary:InfoCodeDict];
    [InfoNameDict setValue:[[Session share] getMemberNickName] forKey:@"0,3"];
    
    [self refreshData];
    [self.table reloadData];
    
}

- (void)viewDidUnload
{
    
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    ///////////////////////////
    [self.table reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {    
    [InfoNameDict release];
    [InfoCodeDict release];

    [table release];
    [super dealloc];
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    NSUInteger row = [indexPath row];
    NSUInteger section = 0;
    
    static NSString *CellIdentifier = @"QuotEditCell";
    
    QuotEditCell* cell = (QuotEditCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"QuotEditCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        
    }
    
    [cell initEditBeginBlcok:^(NSString *txtValue,int section1,int row1)
     {
         NSString *tmp_v=[NSString stringWithString:txtValue];
         NSString *tmp_k=[NSString stringWithFormat:@"%d,%d",section1,row1];
         [InfoNameDict setValue:tmp_v forKey:tmp_k];
         [InfoCodeDict setValue:tmp_v forKey:tmp_k];
     }];
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    cell.row = row;
    cell.section = section;
    //allow edit
    cell.textValue.userInteractionEnabled=YES;
    cell.textValue.delegate = (id)self;
    
    if (row < [self.InfoListTitle count]) 
    {
       cell.labName.text = [self.InfoListTitle objectAtIndex:row];
    }
    
    if (row == 2 || row == 3){
        cell.textValue.userInteractionEnabled=NO;
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;
    }
    
    if ([InfoNameDict count]>0){
        cell.textValue.text = [InfoNameDict objectForKey:[NSString stringWithFormat:@"%d,%d",section,row]];
    }
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    NSInteger section=0;
    NSInteger row=indexPath.row;
    
    QuotSelectList *selectList = [[QuotSelectList alloc] init];
    
    selectList.section=section;
    selectList.row=row;
    
    //2@"leibie，
    if (row == 2){
        [selectList SetInfoType:INFO_TYPE_CUSTOMER];   
    }
    //3danwei
    if (row == 3){
        [selectList SetInfoType:INFO_TYPE_EMPLOYE];   
    }

    [selectList setLastSelectID:[InfoCodeDict objectForKey:[NSString stringWithFormat:@"%d,%d",section, row]]];

    [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
        [InfoNameDict setValue:name forKey:key];
//        NSLog(@"name=%@;code=%@,key=%@",name,code,key);
        [InfoCodeDict setValue:code forKey:key];
//        NSLog(@"InfoNameDict:%@",InfoNameDict);
//        NSLog(@"InfoSaveDict:%@",InfoCodeDict);
    }];
    
    [self.navigationController pushViewController:selectList animated:YES];
    [selectList release];
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
    NSInteger section=0;
    NSInteger row=indexPath.row;
    
    QuotSelectList *selectList = [[QuotSelectList alloc] init];
    
    selectList.section=section;
    selectList.row=row;
    
    //2@"leibie，
    if (row == 2){
        [selectList SetInfoType:INFO_TYPE_CUSTOMER];   
    }
    //3danwei
    if (row == 3){
        [selectList SetInfoType:INFO_TYPE_EMPLOYE];   
    }
    
    [selectList setLastSelectID:[InfoCodeDict objectForKey:[NSString stringWithFormat:@"%d,%d",section, row]]];
    
    [selectList initSelectBeginBlcok:^(NSString *name, NSString *code,NSString *key){
        [InfoNameDict setValue:name forKey:key];
        [InfoCodeDict setValue:code forKey:key];
    }];
    
    [self.navigationController pushViewController:selectList animated:YES];
    [selectList release];    
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if (indexPath.row > 0)
        return indexPath;
    
    return nil;
}

#pragma mark - 
#pragma mark - my func

-(void) OnPreExcute
{
    //    [super OnPreExcute];
}

-(void) DoInBackground
{
    //
}

-(void) OnPostExcute
{
    //    [super OnPostExcute];
}

-(void)refreshData
{    
//    [[[TaskManager Share] GetTaskByType:NewContact Delegate:self] Excute];
}


-(void)save{
    //
    NSString *alert=@"";
    
    if ([[InfoCodeDict objectForKey:@"0,2"] length] < 1)
        alert=@"请选择客户.";
    
    if ([[InfoNameDict objectForKey:@"0,1"] length] < 1)
        alert=@"请输入商机名.";

    if ([[InfoNameDict objectForKey:@"0,0"] length] < 1)
        alert=@"请输入商机编号.";

    if ([alert length]>0){
        UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"提示" message:alert delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        [alertview release];
        return;
    }    
    
    XmlRpc *xml = [[XmlRpc alloc] init];
//    NSString *find_date=[DHCUtil getCurrentDate];
    NSMutableDictionary *tmp;
    tmp = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
           [GUID getGUIDString],@"chance_id",
           [InfoNameDict objectForKey:@"0,0"],@"chance_no",
           [InfoNameDict objectForKey:@"0,1"],@"chance_name",
           [InfoCodeDict objectForKey:@"0,2"],@"customer_id",
//           @"SaleType01",@"seek_mode",
//           find_date,@"find_date",
           @"10",@"status",
           [InfoCodeDict objectForKey:@"0,3"],@"employee_id",nil];
    NSLog(@"tmp=%@",tmp);
    
    [xml InsertChanceChance:tmp];
    
    [tmp release];
    [xml release];
    
    [myToast showWithText:@"Save OK."];
    self.navigationItem.rightBarButtonItem.enabled=NO;
    [[InfoType share] SetFlagFreshView:INFO_TYPE_VIEW_REFRESH];
}

@end
